// pages/login/login.js
import Toast from '@vant/weapp/toast/toast'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    account: '',
    password: '',
    isPrivacy: false,
    showPrivacy: false,
    privacyContractName: '',
    isPrivacyOne: false,
    value1: 0,
  },
  exitMiniProgram() {
    wx.exitMiniProgram()
  },
  handleAgreePrivacyAuthorization() {
    this.setData({
      showPrivacy: false
    })
  },
  handleOpenPrivacyContract() {
    wx.openPrivacyContract({
      success: () => {}, // 打开成功
      fail: () => {}, // 打开失败
      complete: () => {}
    })
  },
  consent(e) {
    // if (!wx.getStorageSync('isPrivacy')) {
    //   console.log(1);
    //   Toast({
    //     message: '请先完成阅读',
    //     duration: 3000
    //   });

    // } else {
    //   this.setData({
    //     isPrivacy: e.detail
    //   })
    // }
    this.setData({
      isPrivacy: e.detail
    })
  },
  getback() {
    wx.navigateBack({
      delta: 1
    })
  },
  //登录
  submit(e) {
    let that = this
    wx.getPrivacySetting({
      success: res => {
        console.log(res) // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
        if (res.needAuthorization) {
          // 需要弹出隐私协议
          this.setData({
            showPrivacy: true
          })
        } else {
          // console.log(that.data.isPrivacy,'登录',this.data.account,'---',this.data.password);
          if (that.data.isPrivacy == false) {
            that.setData({
              isPrivacyOne: true
            })
            if (that.data.isPrivacyOne) {
              setTimeout(() => {
                that.setData({
                  isPrivacyOne: false
                })
              }, 2000)
            }
          } else {
            that.setData({
              isPrivacyOne: false
            })
            Toast.loading({
              message: '登录中...',
              forbidClick: true
            })
            wx.login({
              success(resd) {
                if (resd.code) {
                  wx.getUserInfo({
                    desc: '获取用户的信息', //获取用户的信息
                    success: (res) => { //用户成功授权
                      let userInfo = res.userInfo
                      wx.request({
                        url: app.serverUrl + '/api/login',
                        data: {
                          code: resd.code,
                          avatar: userInfo.avatarUrl,
                          name: userInfo.nickName
                        },
                        method: 'POST',
                        success: (rese => {
                          // console.log(rese);
                          if (rese.data.code == 200) {
                            // console.log(rese.data.data.token);
                            wx.setStorageSync('token', rese.data.data.token)
                            Toast.success({
                              message: '登录成功',
                              forbidClick: true
                            })
                            setTimeout(() => {
                              wx.navigateBack({
                                delta: 1
                              })
                            }, 2000)
                          } else {
                            Toast({
                              message: rese.data.message[0],
                              duration: 3000
                            });
                          }
                        })
                      })
                    },
                    fail: (err) => {
                      console.log(err, '错误');
                    }
                  })

                } else {
                  console.log('登录失败！' + res.errMsg)
                }
              }
            })

          }
          // 用户已经同意过隐私协议，所以不需要再弹出隐私协议，也能调用已声明过的隐私接口
          // wx.getUserProfile()
          // wx.chooseMedia()
          // wx.getClipboardData()
          // wx.startRecord()
        }
      },
      fail: () => {},
      complete: () => {}
    })

  },
  powInput(e) {
    this.setData({
      password: e.detail
    })
  },
  actInput(e) {
    this.setData({
      account: e.detail
    })
  },
  // 隐私协议
  privacyTo() {
    let that = this
    wx.navigateTo({
      url: '/pages/privacy/privacy?isPrivacy=' + (that.data.isPrivacy ? 1 : 0),
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(object) {
    let that = this
    if (object) {
      that.setData({
        isPrivacy: true
      })
      console.log(object.isPrivacy);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})